﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MTGScore.Resources;

namespace MTGScore
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        SavedOptions savedOptions;
        public SettingsPage()
        {
            InitializeComponent();
            savedOptions = Storage.LoadSavedOptions();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSavedOptions();
        }
        private void SaveOptionsState()
        {
            //if (first_loading)
            Storage.SaveSavedOptions(savedOptions);
        }
        private void LoadSavedOptions()
        {
            Alert_Time_Toggle.IsChecked = savedOptions.alert_time;
            Alert_End_Time_Toggle.IsChecked = savedOptions.alert_end_time;
            AllowScreenOffToggle.IsChecked = savedOptions.allow_screen_off;
            Allow_Timer.IsChecked = savedOptions.allow_Timer;
            //CountDownToggle.IsChecked = savedOptions.countDown;
            Allow_Center_End_Game.IsChecked = savedOptions.allow_center_end_game;
            Center_Limit_Area.Text = "" + savedOptions.center_max_value;
            Allow_ShortCuts.IsChecked = savedOptions.allow_shortcut_menu;
            TimePicker.Value = savedOptions.timeSpan;
            AlertTimePicker.Value = savedOptions.alertTimeSpan;
            //Semplified_Toggle.IsChecked = savedOptions.semplified;
            if (savedOptions.timerType == MTGScore.TimerType.Increasing)
                TimerType.SelectedItem = TimerType_Increasing;
            else
                TimerType.SelectedItem = TimerType_Decreasing;
            SetTimerSwitchs(savedOptions.timerType);
            if (savedOptions.allow_center_end_game)
            {
                Center_Limit_Text.Visibility = System.Windows.Visibility.Visible;
                Center_Limit_Area.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                Center_Limit_Text.Visibility = System.Windows.Visibility.Collapsed;
                Center_Limit_Area.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (savedOptions.first_page == 0)
                FirstPageChoose_Area.Text = Strings.Match;
            else if (savedOptions.first_page == 1)
                FirstPageChoose_Area.Text = Strings.Mana;
            else if (savedOptions.first_page == 2)
                FirstPageChoose_Area.Text = Strings.History;
            else if (savedOptions.first_page == 3)
                FirstPageChoose_Area.Text = Strings.SearchCards;
            else if (savedOptions.first_page == 4)
                FirstPageChoose_Area.Text = Strings.Helps;
            else if (savedOptions.first_page == 5)
                FirstPageChoose_Area.Text = Strings.Settings;

            SetTimePickerValue();
            SetAlertTimePickerValue();
        }


        private void Allow_Timer_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_Timer == true)
                return;
            savedOptions.allow_Timer = true;

            SetTimerOptions(true);
            SaveOptionsState();
        }

        void SetTimerOptions(bool on)
        {
            if (on)
            {
                

                //settings page
                Alert_Time_Toggle.Visibility = System.Windows.Visibility.Visible;
                Alert_End_Time_Toggle.Visibility = System.Windows.Visibility.Visible;
                TimerTypeText.Visibility = System.Windows.Visibility.Visible;
                TimerType.Visibility = System.Windows.Visibility.Visible;
                //CountDownToggle.Visibility = System.Windows.Visibility.Visible;
                TimePicker.Visibility = System.Windows.Visibility.Visible;
                TimePickerText.Visibility = System.Windows.Visibility.Visible;
                if (savedOptions.alert_time)
                {
                    AlertTimePicker.Visibility = System.Windows.Visibility.Visible;
                    AlertTimePickerText.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
                    AlertTimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                }
                SetTimerSwitchs(savedOptions.timerType);
            }
            else
            {
                
                //settings page
                Alert_Time_Toggle.Visibility = System.Windows.Visibility.Collapsed;
                Alert_End_Time_Toggle.Visibility = System.Windows.Visibility.Collapsed;
                TimerTypeText.Visibility = System.Windows.Visibility.Collapsed;
                TimerType.Visibility = System.Windows.Visibility.Collapsed;
                //CountDownToggle.Visibility = System.Windows.Visibility.Collapsed;
                TimePicker.Visibility = System.Windows.Visibility.Collapsed;
                TimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
                AlertTimePickerText.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        private void SetTimerSwitchs(TimerType type)
        {

            if (type == MTGScore.TimerType.Increasing)
            {
                TimerType.SelectedItem = TimerType_Increasing;


                Alert_Time_Toggle.Visibility = System.Windows.Visibility.Collapsed;
                Alert_End_Time_Toggle.Visibility = System.Windows.Visibility.Collapsed;

                //CountDownToggle.Visibility = System.Windows.Visibility.Collapsed;
                AlertTimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                TimePicker.Visibility = System.Windows.Visibility.Collapsed;
                TimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
                AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                //if (!start_timer_on_resume)
                //{
                //    timeSpan = savedOptions.timeSpan;
                //}
                TimerType.SelectedItem = TimerType_Decreasing;

                Alert_Time_Toggle.Visibility = System.Windows.Visibility.Visible;
                Alert_End_Time_Toggle.Visibility = System.Windows.Visibility.Visible;
                //AlertTimePickerText.Visibility = System.Windows.Visibility.Visible;
                //CountDownToggle.Visibility = System.Windows.Visibility.Visible;
                TimePicker.Visibility = System.Windows.Visibility.Visible;
                TimePickerText.Visibility = System.Windows.Visibility.Visible;
                if (savedOptions.alert_time)
                {
                    AlertTimePicker.Visibility = System.Windows.Visibility.Visible;
                    AlertTimePickerText.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
                    AlertTimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                }
            }

        }
        private void Allow_Timer_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_Timer == false)
                return;
            if (MessageBox.Show(Strings.ShutDownTimer, "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                savedOptions.allow_Timer = false;
                SetTimerOptions(false);
                SaveOptionsState();
            }
            else
                Allow_Timer.IsChecked = true;
        }
        private void Alert_End_Time_Toggle_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.alert_end_time == true)
                return;
            savedOptions.alert_end_time = true;
            SaveOptionsState();
        }
        private void Alert_End_Time_Toggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.alert_end_time == false)
                return;
            savedOptions.alert_end_time = false;
            SaveOptionsState();
        }
        private void Alert_Time_Toggle_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.alert_time == true)
                return;
            savedOptions.alert_time = true;
            AlertTimePicker.Visibility = System.Windows.Visibility.Visible;
            AlertTimePickerText.Visibility = System.Windows.Visibility.Visible;
            SaveOptionsState();
        }
        private void Alert_Time_Toggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.alert_time == false)
                return;
            savedOptions.alert_time = false;
            AlertTimePicker.Visibility = System.Windows.Visibility.Collapsed;
            AlertTimePickerText.Visibility = System.Windows.Visibility.Collapsed;
            SaveOptionsState();
        }
        private void AllowScreenOffToggle_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_screen_off == true)
                return;
            savedOptions.allow_screen_off = true;
            MessageBox.Show(Strings.RestartRequired);
            SaveOptionsState();
        }
        private void AllowScreenOffToggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_screen_off == false)
                return;

            savedOptions.allow_screen_off = false;
            MessageBox.Show(Strings.RestartRequired);
            SaveOptionsState();
        }
        private void TimerType_SelectionChanged(object sender, RoutedEventArgs e)
        {

            ListBoxItem li = (ListBoxItem)TimerType.SelectedItem;
            string val = (string)li.Content;
            if (val == Strings.Increasing)
            {
                if (savedOptions.timerType == MTGScore.TimerType.Increasing)
                    return;
                savedOptions.timerType = MTGScore.TimerType.Increasing;
                //savedOptions.timeSpan = TimeSpan.Zero;
                //timeSpan = TimeSpan.Zero;
                //TimePickerText.Visibility = System.Windows.Visibility.Collapsed;
                //TimePicker.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                if (savedOptions.timerType == MTGScore.TimerType.Decreasing)
                    return;
                savedOptions.timerType = MTGScore.TimerType.Decreasing;
                //timeSpan = savedOptions.timeSpan;

                //TimePickerText.Visibility = System.Windows.Visibility.Visible;
                //TimePicker.Visibility = System.Windows.Visibility.Visible;
            }
            //UpdateTextAreas();
            SetTimerSwitchs(savedOptions.timerType);
            SaveOptionsState();
        }
        //bool is_opened_time_picker = false;
        private void TimePicker_GotFocus(object sender, RoutedEventArgs e)
        {
            //is_opened_time_picker = true;
            SetTimePickerValue();
        }
        private void AlertTimePicker_GotFocus(object sender, RoutedEventArgs e)
        {
            //is_opened_time_picker = true;
            SetAlertTimePickerValue();
        }
        private void SetAlertTimePickerValue()
        {
            //var now = DateTime.Now;
            var timeSpan = savedOptions.alertTimeSpan;
            AlertTimePicker.Value = timeSpan;
        }
        private void SetTimePickerValue()
        {
            //var now = DateTime.Now;
            var timeSpan = savedOptions.timeSpan;
            TimePicker.Value = timeSpan;
        }
        private void Allow_Center_End_Game_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_center_end_game == true)
                return;
            Center_Limit_Text.Visibility = System.Windows.Visibility.Visible;
            Center_Limit_Area.Visibility = System.Windows.Visibility.Visible;
            savedOptions.allow_center_end_game = true;
            SaveOptionsState();
        }
        private void Allow_Center_End_Game_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.allow_center_end_game == false)
                return;
            Center_Limit_Text.Visibility = System.Windows.Visibility.Collapsed;
            Center_Limit_Area.Visibility = System.Windows.Visibility.Collapsed;
            savedOptions.allow_center_end_game = false;
            SaveOptionsState();
        }
        private void Center_Limit_Lost_Focus_Click(object sender, RoutedEventArgs e)
        {
            Int32 a;
            if (Int32.TryParse(Center_Limit_Area.Text, out a))
                savedOptions.center_max_value = Math.Min(a, 100);
            else
                Center_Limit_Area.Text = "" + savedOptions.center_max_value;
            SaveOptionsState();
        }
        private void TimePicker_ValueChanged(object sender, Coding4Fun.Phone.Controls.Toolkit.ValueChangedEventArgs<TimeSpan> e)
        {
            if (savedOptions.timeSpan == TimePicker.Value.Value)
                return;
            //timer_status = TimerStatus.Stopped;
            //SetTimerControls();

            savedOptions.timeSpan = TimePicker.Value.Value;
            //timeSpan = savedOptions.timeSpan;
            //UpdateTextAreas();
            savedOptions.alertTimeSpan = new TimeSpan(savedOptions.timeSpan.Ticks / 2);
            AlertTimePicker.Value = savedOptions.alertTimeSpan;
            SaveOptionsState();
        }
        private void AlertTimePicker_ValueChanged(object sender, Coding4Fun.Phone.Controls.Toolkit.ValueChangedEventArgs<TimeSpan> e)
        {
            if (savedOptions.alertTimeSpan == AlertTimePicker.Value.Value)
                return;

            savedOptions.alertTimeSpan = AlertTimePicker.Value.Value;
            SaveOptionsState();
        }

        #region STARTPAGE_SELECTOR

        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            try
            {
                Border border = sender as Border;
                ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
                if (contextMenu.Parent == null)
                {
                    contextMenu.IsOpen = true;
                }
            }
            catch (Exception)
            { }
        }
        private void FirstPageChoose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var t = (MenuItem)sender;
                String s = (String)t.Header;

                if (s == Strings.Match)
                    savedOptions.first_page = 0;
                else if (s == Strings.Mana)
                    savedOptions.first_page = 1;
                else if (s == Strings.History)
                    savedOptions.first_page = 2;
                else if (s == Strings.Search)
                    savedOptions.first_page = 3;
                else if (s == Strings.Helps)
                    savedOptions.first_page = 4;
                else if (s == Strings.Settings)
                    savedOptions.first_page = 5;
                FirstPageChoose_Area.Text = s;
                SaveOptionsState();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }


        #endregion


        private void Allow_ShortCuts_Checked(object sender, RoutedEventArgs e)
        {
            savedOptions.allow_shortcut_menu = true;
            SaveOptionsState();
            //CreateApplicationBar();
        }

        private void Allow_ShortCuts_Unchecked(object sender, RoutedEventArgs e)
        {
            savedOptions.allow_shortcut_menu = false;
            SaveOptionsState();
            //CreateApplicationBar();
        }

        private void Semplified_Toggle_Checked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.semplified == true)
                return;

            savedOptions.semplified = true;
            SaveOptionsState();
            MessageBox.Show(Strings.RestartNeeded);
            NavigationService.GoBack();
        }

        private void Semplified_Toggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (savedOptions.semplified == false)
                return;
            savedOptions.semplified = true;
            SaveOptionsState();
            MessageBox.Show(Strings.RestartNeeded);
            NavigationService.GoBack();
        }
    }
}